package com.ymatou.iapi.liveseller.testcase;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.RemoveLiveProductBean;
import com.ymatou.iapi.liveseller.service.RemoveLiveProductCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
/**
*移除商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RemoveLiveProduct{
	private static RemoveLiveProductBean removeliveproductBean;
	private static RemoveLiveProductCall removeliveproductCall;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("移除商品");
	}

	@Before
	public void caseUp() {
		removeliveproductBean=new RemoveLiveProductBean();
		removeliveproductCall=new RemoveLiveProductCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(removeliveproductCall.getOpurl());
		liveRoomWapper=null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveLiveProduct_001() {
		Logger.start(true,"status0未开始直播移除商品成功");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			removeliveproductBean.setLiveId(createlive.getId());
			removeliveproductBean.setProductId(pid);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();			
			Logger.verifyEquals("200", removeliveproductCall.getString("Code"), "验证Code");
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(1,listm.size(), "验证有此数据");
			Logger.verifyEquals(-1, listm.get(0).get("Action"), "验证Action删除");
			
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(0, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(0, liveroom.getLiveProductNum(), "验证LiveProductNum");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RemoveLiveProduct_002() {
		Logger.start(true,"不输入liveid失败");
		try {
			removeliveproductBean.setProductId("1");
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();		
			Logger.verifyEquals("400", removeliveproductCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播Id参数错误", removeliveproductCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RemoveLiveProduct_003() {
		Logger.start(true,"不输入ProductId失败");
		try {
			removeliveproductBean.setLiveId(1);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();		
			Logger.verifyEquals("400", removeliveproductCall.getString("Code"), "验证Code");
			Logger.verifyEquals("商品Id不能为空", removeliveproductCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RemoveLiveProduct_004() {
		Logger.start(true,"liveid和ProductId不匹配不操作");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			removeliveproductBean.setLiveId(createlive.getId()-1);
			removeliveproductBean.setProductId(pid);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();			
			Logger.verifyEquals("201", removeliveproductCall.getString("Code"), "验证Code");
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(1,listm.size(), "验证有此数据");
			Logger.verifyEquals(0, listm.get(0).get("Action"), "验证Actionm没有删除");
			
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(1, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(1, liveroom.getLiveProductNum(), "验证LiveProductNum");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_RemoveLiveProduct_005() {
		Logger.start(true,"ProductId不存在不操作");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			removeliveproductBean.setLiveId(createlive.getId());
			removeliveproductBean.setProductId(pid+1);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();			
			Logger.verifyEquals("200", removeliveproductCall.getString("Code"), "验证Code");
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(1,listm.size(), "验证有此数据");
			Logger.verifyEquals(0, listm.get(0).get("Action"), "验证Actionm没有删除");
			
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(1, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(1, liveroom.getLiveProductNum(), "验证LiveProductNum");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RemoveLiveProduct_006() {
		Logger.start(true,"status9直播推流结束移除商品失败");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 9);
			removeliveproductBean.setLiveId(createlive.getId());
			removeliveproductBean.setProductId(pid);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();			
			Logger.verifyEquals("201", removeliveproductCall.getString("Code"), "验证Code");
			Logger.verifyEquals("直播已关闭或已结束", removeliveproductCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RemoveLiveProduct_007() {
		Logger.start(true,"status2推流直播移除商品成功");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			removeliveproductBean.setLiveId(createlive.getId());
			removeliveproductBean.setProductId(pid);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();			
			Logger.verifyEquals("200", removeliveproductCall.getString("Code"), "验证Code");
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(1,listm.size(), "验证有此数据");
			Logger.verifyEquals(-1, listm.get(0).get("Action"), "验证Action删除");
			
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(0, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(0, liveroom.getLiveProductNum(), "验证LiveProductNum");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RemoveLiveProduct_008() {
		Logger.start(true,"status3断流直播移除商品成功");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 3);
			removeliveproductBean.setLiveId(createlive.getId());
			removeliveproductBean.setProductId(pid);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();			
			Logger.verifyEquals("200", removeliveproductCall.getString("Code"), "验证Code");
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(1,listm.size(), "验证有此数据");
			Logger.verifyEquals(-1, listm.get(0).get("Action"), "验证Action删除");
			
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(0, liveroom.getPutawayCount(), "验证PutawayCount");
			Logger.verifyEquals(0, liveroom.getLiveProductNum(), "验证LiveProductNum");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveLiveProduct_009() {
		Logger.start(true,"直播移除商品再次添加成功");
		try {
			//删除再添加会有2条记录 一条action-1已删除标记
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			removeliveproductBean.setLiveId(createlive.getId());
			removeliveproductBean.setProductId(pid);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();			
			Logger.verifyEquals("200", removeliveproductCall.getString("Code"), "验证Code");
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(1,listm.size(), "验证有此数据");
			Logger.verifyEquals(-1, listm.get(0).get("Action"), "验证Action删除");
			Thread.sleep(1000);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(0, listm.get(0).get("Action"), "验证Action删除");
			Logger.debug(listm);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_RemoveLiveProduct_010() {
		Logger.start(true,"ProductId不在直播列表中移除,product存在");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
//			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			removeliveproductBean.setLiveId(createlive.getId());
			removeliveproductBean.setProductId(pid);
			removeliveproductCall.setData(removeliveproductBean);
			removeliveproductCall.callService();			
			Logger.verifyEquals("200", removeliveproductCall.getString("Code"), "验证Code");
			List<Map> listm = liveRoomWapper.selectLiveproductByIdAndPid(createlive.getId(), pid);
			Logger.verifyEquals(0,listm.size(), "验证无此数据");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}